# coding:utf-8
from modules.common import d_common,getsource
from modules.baidu.dict_baiduoption import dict_baiduoption
from common.iniconfig import iniconfig
from modules.common import delete_file_folder
from common.read.play import dict_play
import cPickle as p
import re
import gettext
import types
import urllib
import os
import zipfile
import shutil
import sys
import string
import md5

class d_baidu_re:
        source=""
        word=""
        ct=""
        ph=""
        bk=""
        sy=""
        an=""
        es=""
        cs=""
	pr=""
	ne=""
        wr=""
        
        def __init__(self,word):
		for uchar in word.replace(" ",""):
			if not((uchar >= u'\u0041' and uchar<=u'\u005a') or (uchar >= u'\u0061' and uchar<=u'\u007a')):
				raise UnicodeError		
                self.word=word
        
        def __del__(self):
                pass
        
        def _get_ct(self,source):
                pattern = re.compile(r'''<div class="tab en-simple-means dict-en-simplemeans-english" id="en-simple-means">(.*)<div class="source">以下结果由 <img src="/static/img/jinshan.png"><a href='http://cp.iciba.com/' target="_blank">金山词霸</a> 提供</div></div>''')
                match = pattern.findall(source)
                if match:
                        self.ct=match[0]
                        self.ct=self.ct.replace("<span>","<br/><span>")
                        self.ct=self.ct.replace("</strong><br/>","</strong>")
                        self.ct=self.ct.replace("<p><br/>","<p>")
                        self.ct=self.ct.replace("</a>"," ")
                        pattern2=re.compile(r'''<a href=[^>]*>''')
                        self.ct=pattern2.sub(" ",self.ct)
                        
        def get_ct(self):
                return self.ct
         
        def _get_es(self,source):
                pattern1=re.compile(r'''var example_data =(.*)]];''')
                match1=pattern1.findall(source)
                if match1:
                        temp=[]
                        temp1=[]
                        a=""
                        match=match1[0]
                        match=match[2:]
                        match2=match.split(r"],[[[")
                        for m in match2:
                                temp.append(r'['+m)
                        for m in temp:
                                match3=m.split(r"]],[[")
                                x=match3[0]+r"]"
                                y=r"["+match3[1]
                                pattern4=re.compile(r'\["(.*?)",')
                                match4=pattern4.findall(x)
				match5=pattern4.findall(y)
                                for m in match4:
                                        a=a+m+" "
                                a=a[:-1]
                                temp1.append(a)                
                                a=""
                                for m in match5:
                                        a=a+m+" "
                                a=a[:-1]
                                temp1.append(a)   
                                a=""
                        
                        i=0
                        a="<ul>"
                        for m in temp1:
                                i=i+1
                                if i%2==1:
                                        a=a+"<li>"+m
                                else:
                                        a=a+"<br>"+m.replace(" ","")+"<br></li>"
                        a=a+"</ul>"
                        a=a.replace("\\'","'").replace('\\"','"').replace("\\\\","\\").replace(self.word,"<font color=Red><strong>%s</strong></font>"%self.word)
                        self.es=a                                     
                        
        def get_es(self):
                return self.es
        
        def _get_ph(self,source):
                pattern = re.compile(r'''<div class="tab en-phrase dict-en-phrase-english" id="en-phrase">(.*)<div class="source">以下结果由 <img src="/static/img/jinshan.png"><a href='http://cp.iciba.com/' target="_blank">金山词霸</a> 提供''')
                match = pattern.findall(source)
                if match:
                        self.ph=match[0]
                
        def get_ph(self):
                return self.ph        
        
        def _get_bk(self,source):
                pattern = re.compile(r'''<div class="tab en-baike-mean dict-en-baikemean-english" id="en-baike-mean">(.*)查看更多>></a></div> </div>''')
                match = pattern.findall(source)
                if match:
                        self.bk=match[0]+_("Read More")+'''>></a></div> </div>'''
                        self.bk=self.bk.replace("<span>","<br/><span>")
                        self.bk=self.bk.replace("</strong><br/>","</strong>")
                        self.bk=self.bk.replace("<p><br/>","<p>")
                
        def get_bk(self):
                return self.bk
	
        def _get_ne(self,source):
		source=source.replace("<dl><dd><ol>","<ul>")
		source=source.replace("</ol></dd></dl>","</ul>")
		temp=r'''<div class="tab en-net-means dict-en-netmeans-english" id="en-net-means">'''
		temp2=r'''<div id="more-netmeans" class="more-result">'''
		a=source.find(temp)
		b=source.find(temp2,a)
                if a!=-1 and b!=-1:
                        self.ne=source[a+len(temp):b]
                
        def get_ne(self):
                return self.ne  
        
        def _get_sy(self,source):
                a=str(source).find("<dt>同义词</dt>")
                b=str(source).find("</dl>",a)
                if a!=-1 and b!=-1:
                        self.sy=source[a:b]
                        self.sy=self.sy.replace("</a>","&nbsp;&nbsp;&nbsp;")
                        pattern2=re.compile(r'''<a href=[^>]*>''')
                        self.sy=pattern2.sub(" ",self.sy)                        
                
        def get_sy(self):
                return self.sy        
        
        def _get_an(self,source):
                a=str(source).find("<dt>反义词</dt>")
                b=str(source).find("</dl>",a)
                if a!=-1 and b!=-1:
                        self.an=source[a:b]
                        self.an=self.an.replace("</a>","&nbsp;&nbsp;&nbsp;")
                        pattern2=re.compile(r'''<a href=[^>]*>''')
                        self.an=pattern2.sub(" ",self.an)        
                
        def get_an(self):
                return self.an         
	
	def _get_pr(self,source):
			pattern=re.compile(r'''\[英\]<b lang="EN-US" xml:lang="EN-US">(.*?)</b>''')         
			match=pattern.findall(source)
			pattern2=re.compile(r'''\[美\]<b lang="EN-US" xml:lang="EN-US">(.*?)</b>''')
			match2=pattern2.findall(source)
			pron_temp=""
			if match:
				pron_temp="[英]  " + match[0]
			if match2:
				if pron_temp.strip()!="":
					pron_temp=pron_temp+" &nbsp;&nbsp;[美]  "+match2[0]
				else:
					pron_temp="[美]  "+match2[0]
			self.pr= pron_temp        
	def get_pr(self):
		return self.pr
        
        def _get_cs(self,source):
                a=str(source).find('<div class="tab en-collins dict-en-collins-english" id="en-collins">')
                b=str(source).find('''<div class="source">以下结果由 <img src="/static/img/jinshan.png"><a href='http://cp.iciba.com/' target="_blank">金山词霸</a> 提供</div><div id="more-collins" class="more-result"><a href='#'>查看更多</a></div></div>''',a)
                if a!=-1 and b!=-1:
                        self.cs=source[a:b].replace("</a>","")+("</div>")
                        pattern=re.compile(r'''<a\s*[^>]*\s*href=[^>]*\s*[^>]*\s*>''')
                        self.cs=pattern.sub(" ",self.cs)                        
                        pattern=re.compile(r'''<a.*?href="#">''')
                        self.cs=pattern.sub(" ",self.cs)
                
        def get_cs(self):
                return self.cs
        
        def _get_wrong(self,source):
		a=source.find('<div class="suggest-list"><strong>您要找的是不是？</strong>')
		if a==-1:
			self.wr=""
			return
		b=source.find('</div>',a)
		self.wr="<br>"+source[a:b]+"</div>"
		self.wr=self.wr.replace("</strong>","</strong><br>")
		self.wr=self.wr.replace("</a>","</a><br>")
		
        
        def get_wrong(self):
		return self.wr
        
        def analyse(self):
                source=getsource("http://dict.baidu.com/s?wd=%s" % self.word)
		self._get_wrong(source)
                self._get_ct(source)
                self._get_es(source)
                self._get_bk(source)
                self._get_cs(source)
                self._get_ph(source)
                self._get_sy(source)
                self._get_an(source)
		self._get_pr(source)
		self._get_ne(source)
		self.word=self.word+self.wr
		if self.wr!="":	
			return False
		else:
			return True
	
	def __getitem__(self,key):
		if key=="Word":
			return self.word
		if key=="Pronounce":
			return self.pr
		if key=="Concise translate":
			return self.ct
		if key=="Collins":
			return self.cs
		if key=="Synonym":
			return self.sy	
		if key=="Antonym":
			return self.an				
		if key=="Phrases":
			return self.ph		
		if key=="Baike":
			return self.bk			
		if key=="NetMeans":
			return self.ne		
		if key=="Example Sentence":
			return self.es

class d_baidu(d_common):
        cont=[]
	cont_en=[]
        #cont= [_("Word"),_("Concise translate"),_("Collins"),_("Synonym"),_("Antonym"),_("Phrases"),_("Baike"),_("Example Sentence")]
       
        def __init__(self):
                d_common.__init__(self,_("BaiduDict"),_("All data in this dict is from Baidu."),"hzh","v1.0.0","2013-02-03")
                self.cont.append(_("Word"))
		self.cont.append(_("Pronounce"))
                self.cont.append(_("Concise translate"))
                self.cont.append(_("Collins"))
                self.cont.append(_("Synonym"))
                self.cont.append(_("Antonym"))
                self.cont.append(_("Phrases"))
                self.cont.append(_("Baike"))
		self.cont.append(_("NetMeans"))
                self.cont.append(_("Example Sentence"))
		
		
		self.cont_en.append("Word")
		self.cont_en.append("Pronounce")
		self.cont_en.append("Concise translate")
		self.cont_en.append("Collins")
		self.cont_en.append("Synonym")
		self.cont_en.append("Antonym")
		self.cont_en.append("Phrases")
		self.cont_en.append("Baike")
		self.cont_en.append("NetMeans")
		self.cont_en.append("Example Sentence")
		
		self.baidu_ini=iniconfig("modules/baidu/settings")
		pathzip="modules/baidu/downloads/baidu_data.zip"
		if not os.path.exists(pathzip):
			tmp=zipfile.ZipFile(pathzip,"w")
			tmp.close()
			self.zip_file=zipfile.ZipFile(pathzip,"a")
			self.zip_list=self.zip_file.namelist()
		else:
			self.zip_file=zipfile.ZipFile(pathzip,"a")
			self.zip_list=self.zip_file.namelist()
	
	def __del__(self):
		try:
			self.zip_file.close()
			delete_file_folder("modules/baidu/downloads","baidu_data.zip")
		except Exception,e:
			pass	
		
        def __str__(self):
                return d_common.__str__(self)
        
        def setoptiongui(self,parent):
		a=dict_baiduoption(parent)
		a.ShowModal()
        
        def setoption(self,op,val):
		self.baidu_ini.set_item(op,val)
		
	def getoption(self,op):
		return self.baidu_ini.get_item(op)
		     
        
        def getcontent(self,word):
                temp=d_baidu_re(word)
                temp.analyse()
                return {self.cont_en[0]:temp.word,self.cont_en[1]:temp.pr,self.cont_en[2]:temp.ct
                        ,self.cont_en[3]:temp.cs,self.cont_en[4]:temp.sy,self.cont_en[5]:temp.an
                        ,self.cont_en[6]:temp.ph,self.cont_en[7]:temp.bk,self.cont_en[8]:temp.ne,self.cont_en[9]:temp.es}
        
        def get_li(self,name,str):
		if str==None:
			return ""
                if str.strip()=="":
                        return ""
                else:
			if name==self.cont[1]:
				return '''<li><br/>%s</li>''' % str
                        else:
				return '''<li><font color=Blue><h3>%s</h3></font><br/>%s</li>''' % (name,str)
         
        def getmp3(self,word,parent):
		read=dict_play(parent)
		path="modules/baidu/downloads/%s.mp3" % word
		name=word+".mp3"
		if not os.path.exists(path):
			if name in self.zip_list:
				self.zip_file.extract(name,"modules/baidu/downloads/")
			else:
				source=getsource("http://dict.baidu.com/s?wd=%s" % word)
				pattern=re.compile(r'''<a href="#"\s*url="([^>]*).mp3"\s*>''')
				match=pattern.findall(source)
				if match:
					match[0]=match[0].replace("\n","")
					match[0]=match[0].replace("\r","")						
					urllib.urlretrieve(match[0]+".mp3", path)					
 
			
		if os.path.exists(path):
			parent.m_statusBar1.SetStatusText(_("'%s' is reading")% word,0)
			fil=file(path,"rb")
			fil2=file("modules/baidu/temp/temp.mp3","wb")
			fil2.write(fil.read())
			fil.close()
			fil2.close()
			read.read(os.getcwd()+"/modules/baidu/temp/temp.mp3".replace("/",os.path.sep))
			os.remove("modules/baidu/temp/temp.mp3")
			if self.getoption("auto_save").strip()=="1":	
				if name in self.zip_file.namelist():	
					return
				self.zip_file.write(path,name,zipfile. ZIP_DEFLATED)
				
		else:
			parent.m_statusBar1.SetStatusText(_("'%s' has no mp3 sound.") % word,0)
		del read
	def getwordlist(self):
		a=open("modules/baidu/words.txt","r")
		b=[]
		for i in a.readlines():
			b.append(i.replace("-"," "))
		a.close()
		return b
		
        def getcontentHTML(self,word):
		path="modules/baidu/downloads/%s" % word
		if os.path.exists(path):
			temp2=file(path,"r")
			temp=p.load(temp2)
			temp2.close()
		else:
			if word in self.zip_list:
				self.zip_file.extract(word,"modules/baidu/downloads/")
				temp3=file(path,"r")
				temp=p.load(temp3)
				temp3.close()
			else:
				temp=d_baidu_re(word)
				temp.analyse()					
            
		
                a='''<h1>%s</h1><br/>
                %s
                <ul>
                %s%s%s%s%s%s%s%s
                </ul>
                ''' % (temp[self.cont_en[0]],self.get_li(self.cont[1],temp[self.cont_en[1]]),
		       self.get_li(self.cont[2],temp[self.cont_en[2]]),
		       self.get_li(self.cont[3],temp[self.cont_en[3]]),
		       self.get_li(self.cont[4],temp[self.cont_en[4]]),
                       self.get_li(self.cont[5],temp[self.cont_en[5]]),
		       self.get_li(self.cont[6],temp[self.cont_en[6]]),
		       self.get_li(self.cont[7],temp[self.cont_en[7]]),
		       self.get_li(self.cont[8],temp[self.cont_en[8]]),
		       self.get_li(self.cont[9],temp[self.cont_en[9]]))
                if self.getoption("auto_save").strip()=="1":

			if word in self.zip_list:
				return a
			
			b={self.cont_en[0]:temp.word,self.cont_en[1]:temp.pr,self.cont_en[2]:temp.ct
                                      ,self.cont_en[3]:temp.cs,self.cont_en[4]:temp.sy,self.cont_en[5]:temp.an
                                      ,self.cont_en[6]:temp.ph,self.cont_en[7]:temp.bk,self.cont_en[8]:temp.ne,self.cont_en[9]:temp.es}
			self.zip_file.writestr(word,p.dumps(b),zipfile.ZIP_DEFLATED)
			self.zip_list.append(word)
			del b
                return a
	
	def download(self,input,output,has_mp3,append,word_start=0,mp3_start=0):
		
		
		if append:
			tmp=zipfile.ZipFile(output,"a")
		else:
			tmp=zipfile.ZipFile(output,"w")
			if os.path.exists(output+".project"):
				os.remove(output+".project")
		hehe=iniconfig(output+".project")	
		a=file(input,"r")
		b=a.readlines()
		allnums=len(b)
		
		if hehe.get_item("word")!="":
			if str(md5.new(a.read()).digest())!= hehe.get_item("md5"):
				print _("\n**error:Project file %s refers to another file,please remove it.**\n") % output+".project"
				return
			word_start=string.atoi(hehe.get_item("word"))
		else:
			hehe.set_item("md5",str(md5.new(a.read()).digest()))
		if hehe.get_item("mp3")!="":
			mp3_start=string.atoi(hehe.get_item("mp3"))
		a.close()	
		print _("Begin downloading words' data\n")
		try:
			for c in range(word_start,allnums):
				b[c]=b[c].strip()
				if b[c]=="":
					continue
				percent=100*(c+1)/allnums
				print _("current word:")+b[c]+" "*(25-len(b[c])) +_("percent:")+str(percent)+"%",
				sys.stdout.write("\r")
				try:
					temp=d_baidu_re(b[c])
					temp.analyse()			
					d={self.cont_en[0]:temp.word,self.cont_en[1]:temp.pr,self.cont_en[2]:temp.ct
                        	              ,self.cont_en[3]:temp.cs,self.cont_en[4]:temp.sy,self.cont_en[5]:temp.an
                        	              ,self.cont_en[6]:temp.ph,self.cont_en[7]:temp.bk,self.cont_en[8]:temp.ne,self.cont_en[9]:temp.es}		
					tmp.writestr(b[c],p.dumps(d),zipfile. ZIP_DEFLATED)
				except KeyboardInterrupt,e:
					print _("\nDownload is canceled by user.\n")
					tmp.close()
					return
				except:
					pass
				hehe.set_item("word",str(c+1))
			#print _("current word:")+b[c]+" "*(25-len(b[c])) +_("percent:")+str(percent)+"%",
			#sys.stdout.write("\r")
			print _("\nAll words' data downloads successfully!\n")
			if has_mp3:
				if not os.path.isdir("temp"):
				        os.mkdir("temp")
				pattern=re.compile(r'''<a href="#"\s*url="([^>]*).mp3"\s*>''')
				print _("Now turns to MP3 data...\n")
				for c in range(mp3_start,allnums):
					if b[c]=="":
						continue
					b[c]=b[c].replace("\n","")
					percent=100*(c+1)/allnums
					print _("current word:")+b[c]+" "*(25-len(b[c])) +_("percent:")+str(percent)+"%",
					sys.stdout.write("\r")		
					try:
						source=getsource("http://dict.baidu.com/s?wd=%s" % b[c])
						match=pattern.findall(source)
						if match:
							match[0]=match[0].replace("\n","")
							match[0]=match[0].replace("\r","")
							urllib.urlretrieve(match[0]+".mp3", "temp/%s.mp3"%b[c])	
						if os.path.exists("temp/%s.mp3"%b[c]):
							tmp.write("temp/%s.mp3"%b[c],b[c]+".mp3",zipfile. ZIP_DEFLATED)
							hehe.set_item("mp3",str(c+1))
					except KeyboardInterrupt,e:
						print _("\nDownload is canceled by user.\n")
						tmp.close()
						delete_file_folder("temp")
						os.rmdir("temp")
						return					
					except:
						pass
		
			        try:
				        delete_file_folder("temp")
				        os.rmdir("temp")
				except Exception,e:
					pass
			        print _("\nAll MP3 data download successfully!\n")
			        print _("In all, the zip file should be placed in modules/baidu/downloads directory,\nand named 'baidu_data.zip'\n")
		except KeyboardInterrupt,e:
			print _("\nDownload is canceled by user.\n")
			tmp.close()
			return
		os.remove(output+".project")
		tmp.close()

